package com.jimmy.medical.service.impl;

import com.jimmy.medical.base.impl.JpaSpecificationServiceImpl;
import com.jimmy.medical.entity.FlowOrderType;
import com.jimmy.medical.entity.OrderType;
import com.jimmy.medical.repository.FlowOrderTypeRepository;
import com.jimmy.medical.repository.OrderTypeRepository;
import com.jimmy.medical.service.IDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class DictServiceImpl extends JpaSpecificationServiceImpl<FlowOrderTypeRepository, FlowOrderTypeRepository, FlowOrderType, Long>
        implements IDictService {

    @Autowired
    private FlowOrderTypeRepository flowOrderTypeRepository;
    @Autowired
    private OrderTypeRepository orderTypeRepository;

    private Map<Long, FlowOrderType> flowOrderTypeMap = new HashMap<Long, FlowOrderType>();
    private Map<Long, OrderType> orderTypeMap = new HashMap<Long, OrderType>();

    @PostConstruct
    public void init() {
        List<FlowOrderType> flowOrderTypeList = IteratorUtils.toList(flowOrderTypeRepository.findAll().iterator());
        for (FlowOrderType t : flowOrderTypeList) {
            flowOrderTypeMap.put(t.getId(), t);
        }

        List<OrderType> orderTypeList = IteratorUtils.toList(orderTypeRepository.findAll().iterator());
        for (OrderType t : orderTypeList) {
            orderTypeMap.put(t.getId(), t);
        }
    }


    @Override
    public FlowOrderType getFlowOrderType(Long flowOrderTypeId) {
        if (flowOrderTypeMap.containsKey(flowOrderTypeId)) {
            return flowOrderTypeMap.get(flowOrderTypeId);
        }
        return null;
    }

    @Override
    public OrderType getOrderType(Long orderTypeId) {
        if (orderTypeMap.containsKey(orderTypeId)) {
            return orderTypeMap.get(orderTypeId);
        }
        return null;
    }
}